package TUILayer;

import java.io.IOException;

import ModelLayer.DataStorage;
import ModelLayer.RealException;

public class MainMenuUI {

	private InputParser inputParser;
	private LoginInfo loginInfo;
	private SalesUI salesUI;
	private PersonUI personUI;
	private GroupUI groupUI;
	private SaleItemUI saleItemUI;
	private LeaseItemUI leaseItemUI;
	private LeaseUI leaseUI;
	private StatisticsUI statisticsUI;
	
	public MainMenuUI() throws RealException {
		inputParser = new InputParser();
		this.loginInfo = new LoginInfo(inputParser);
		salesUI = new SalesUI(inputParser, loginInfo);
		personUI = new PersonUI(inputParser, loginInfo);
		groupUI = new GroupUI(inputParser, loginInfo);
		saleItemUI = new SaleItemUI(inputParser, loginInfo);
		leaseItemUI = new LeaseItemUI(inputParser, loginInfo);
		leaseUI = new LeaseUI(inputParser, loginInfo);
		statisticsUI=new StatisticsUI(inputParser);
		start();
	}

	public void start() throws RealException {
		while(loginInfo.getLoggedEmployeeId() == 0)
		{
			loginInfo.start();
		}
		switch(loginInfo.getLoggedEmployeePositionId())
		{
			case 1:
			{
				boolean exit = false;
				while (!exit) {
					int choise = inputParser
							.getInt("\f *** MainMenu ***\n\n1. Sale\n2. Lease\n3. Person\n4. Product\n5. LeaseItem\n6. Group\n7. Statistics\n8. Close\nPlease make your choise: ");
					switch (choise) {
					case 1:
					{
						salesUI.start();
						break;
					}
					case 2: 
						leaseUI.start();
						break;
					case 3: 
					{
						boolean exitPerson = false;
						while (!exitPerson) {
							int personChoise = inputParser
									.getInt("\f *** PersonMenu ***\n\n1. Customer\n2. Contractor\n3. Employee\n4. Return\n\nPlease make your choise: ");

							if (personChoise > 0 && personChoise < 4) {
								personUI.start(personChoise);
							} else if (personChoise == 4) {
								exitPerson = true;
							}
						}
						break;
					}
					case 4: {
						saleItemUI.start();
						break;
					}
					case 5: {
						leaseItemUI.start();
						break;
					}
					case 6: {
						groupUI.start();
						break;
					}
					case 7:{
						statisticsUI.start();
						break;
					}
					case 8: {
						exit = true;
						writeEnd();
						break;
					}
					}
				}
				break;
			}
			case 2:
			{
				boolean exit = false;
				while (!exit) {
					int choise = inputParser
							.getInt("\f *** MainMenu ***\n\n1. Sale\n2. Lease\n3. Person\n4. Product\n5. LeaseItem\n6. Group\n7. Statistics\n8. Close\nPlease make your choise: ");
					switch (choise) {
					case 1:
					{
						salesUI.start();
						break;
					}
					case 2: 
						leaseUI.start();
						break;
					case 3: 
					{
						boolean exitPerson = false;
						while (!exitPerson) {
							int personChoise = inputParser
									.getInt("\f *** PersonMenu ***\n\n1. Customer\n2. Contractor\n3. Employee\n4. Return\n\nPlease make your choise: ");

							if (personChoise > 0 && personChoise < 4) {
								personUI.start(personChoise);
							} else if (personChoise == 4) {
								exitPerson = true;
							}
						}
						break;
					}
					case 4: {
						saleItemUI.start();
						break;
					}
					case 5: {
						leaseItemUI.start();
						break;
					}
					case 6: {
						groupUI.start();
						break;
					}
					case 7:{
						statisticsUI.start();
						break;
					}
					case 8: {
						exit = true;
						writeEnd();
						break;
					}
					}
				}
				break;
			}
			case 3:
			{
				boolean exit = false;
				while (!exit) {
					int choise = inputParser
							.getInt("\f *** MainMenu ***\n\n1. Sale\n2. Lease\n3. Customer\n4. Product\n5. LeaseItem\n6. Group\n7. Close\nPlease make your choise: ");
					switch (choise) {
					case 1:
					{
						salesUI.start();
						break;
					}
					case 2: 
						leaseUI.start();
						break;
					case 3: 
					{
						personUI.start(1);
						break;
					}
					case 4: {
						saleItemUI.start();
						break;
					}
					case 5: {
						leaseItemUI.start();
						break;
					}
					case 6: {
						groupUI.start();
						break;
					}
					case 7: {
						exit = true;
						writeEnd();
						break;
					}
					}
				}
				break;
			}
			
		}
	}

	private void writeEnd() {
		System.out.println("\f *** MainMenu ***");
		System.out.println("\nThe System is shuting down");
		try {
			Thread.sleep(700);
		} catch (InterruptedException e) {

		}
		try {
			DataStorage dataStorage = new DataStorage();
			dataStorage.saveAll();
		} catch (IOException e) {
		}
		System.exit(1);
	}

}
